package cn.bdqn.web.filter;

import cn.bdqn.pojo.SysUser;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class AuthorizationFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse
            servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request=(HttpServletRequest)servletRequest;
        HttpServletResponse response=(HttpServletResponse)servletResponse;
            //判断用户是否登录
        SysUser sysUser=(SysUser)
                request.getSession().getAttribute("loginUser");
        if(sysUser==null){
            //引导用户来到登录页面
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
            out.write("<script type='text/javascript'>" +
                    "alert('请先进行登陆，再进行后续操作！(Filter控制)');" +
                    "window.location.href='login.jsp';" +
                    "</script>");
        }else{
            filterChain.doFilter(request,response);
        }
    }
    @Override
    public void destroy() {
    }
}
